package com.k2data.test.testinstance;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * JUnit test instance lifecycle
 * execute all test methods on the same test instance
 * 
 * @author wangchuan Jan 24, 2019
 *
 */
@RunWith(JUnitPlatform.class)
@DisplayName("Test Instance Lifecycle")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class InstanceLifecycleTest_PerClass {
	private static final Logger log = LoggerFactory.getLogger(InstanceLifecycleTest_PerClass.class);
	
	private int score;
	
	public InstanceLifecycleTest_PerClass(){
		log.info("Constructor");
		this.score = 0;
	}
	
	@BeforeAll
	  public static void init() {
		log.info("@BeforeAll: init()");
	  }

	  @AfterAll
	  public static void done() {
		  log.info("@AfterAll: done()");
	  }

	  @BeforeEach
	  public void setUp() throws Exception {
		  log.info("@BeforeEach: setUp()");
	  }

	  @AfterEach
	  public void tearDown() throws Exception {
		  log.info("@AfterEach: tearDown()");
	  }
	  
	  @Test
	  public void testAdd(){		 
		  log.info(this.toString() + " testAdd, score before: " + this.score);
		  this.score++;
		  log.info(this.toString() + " testAdd, score after: " + this.score);
	  }
	  
	  @Test
	  public void testMinus(){
		  log.info(this.toString() + " testMinus, score before: " + this.score);
		  this.score--;
		  log.info(this.toString() + " testMinus, score after: " + this.score);
	  }
}
